package com.zrrd.jt_admin.image.controller;


import com.zrrd.jt_admin.common.entity.Image;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.ImagePageVo;
import com.zrrd.jt_admin.image.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhangYK
 * @since 2024-07-03
 */
@Api(tags = "07.02、图片管理-接口文档")
@RestController
@RequestMapping("admin/image")
@ResponseBody
public class ImageController {

    @Resource
    private ImageService imageService;

    @ApiOperation(value = "1、图片列表")
    @GetMapping("{page}")
    public ResponseResult findByPage(@PathVariable Integer page,
                                     @RequestParam(required = false) Integer limit) {
        if (limit == null){
            limit = 5;
        }
        ImagePageVo imagePageVo = imageService.findByPage(page,limit);
        return ResponseResult.queryResponse(imagePageVo);
    }

    @ApiOperation(value = "2、图片上传")
    @PostMapping("upload/{imageClassId}")
    public ResponseResult upload(@RequestParam("file") MultipartFile file,
                                 @PathVariable("imageClassId") Integer imageClassId) {

        Image image = imageService.upload(file, imageClassId);

        boolean flag = imageService.save(image);

        if (flag)
            return ResponseResult.defaultResponse(image, "上传成功", "上传失败");
        else
            return ResponseResult.defaultResponse(null, "上传成功", "上传失败");
    }

    @ApiOperation(value = "3、修改图片名称")
    @PutMapping("{id}")
    public ResponseResult updateName(@PathVariable("id") Integer id,
                                     @RequestParam("name") String name){
        boolean flag = imageService.lambdaUpdate()
                .eq(Image::getId, id)
                .set(Image::getName, name)
                .update();
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "4、删除单张图片")
    @DeleteMapping("{id}/delete")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        Boolean flag = imageService.remove(id);
        return ResponseResult.removeResponse(flag);
    }

    @ApiOperation(value = "5、批量删除图片")
    @DeleteMapping("deleteAll")
    public ResponseResult deleteAll(@RequestBody List<Integer> ids) {
        Boolean flag = imageService.removes(ids);
        return ResponseResult.removeResponse(flag);
    }

}

